/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RatingRest.java
 *
 * Created on Feb 20, 2012, 11:33:36 PM
 */
package handax.GUI;

import handax.controller.DBManager;
import handax.entities.Rating;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author aapost
 */
public class RatingRest extends javax.swing.JFrame {

    private String username;
    private int restId;
    private String restName;
    private SearchRes root;
    DBManager dBM = new DBManager();
    DecimalFormat df = new DecimalFormat("#.##");
     
    /** Creates new form RatingRest */
    public RatingRest(int restId, String restName, final String username) {
        this.restId = restId;
        this.restName = restName;
        this.username = username;
       
        initComponents();

        restNameLabel.setText(this.restName);

        if (dBM.checkIfRatingExist(restId, username)) {
            Rating r = dBM.retrieveRating(restId, username);
            System.out.println(r.getQuality());
            qualityStarRater.setRating(r.getQuality());
            priceStarRater.setRating(r.getPrice());
            serviceTimeStarRater.setRating(r.getServiceTime());
            averageRatingLabel.setText(df.format(r.getAverageRating()) + "/10");
            averageRatingStarRater.setRating(r.getAverageRating());

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        restNameLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        saveRatingButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        qualityStarRater = new handax.utilities.StarRater();
        priceStarRater = new handax.utilities.StarRater();
        averageRatingLabel = new javax.swing.JLabel();
        serviceTimeStarRater = new handax.utilities.StarRater();
        averageRatingStarRater = new handax.utilities.StarRater();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(handax.HandaxApp.class).getContext().getResourceMap(RatingRest.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        restNameLabel.setBackground(resourceMap.getColor("restName.background")); // NOI18N
        restNameLabel.setFont(resourceMap.getFont("restName.font")); // NOI18N
        restNameLabel.setText(resourceMap.getString("restName.text")); // NOI18N
        restNameLabel.setName("restName"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        saveRatingButton.setText(resourceMap.getString("SaveRatingButton.text")); // NOI18N
        saveRatingButton.setName("SaveRatingButton"); // NOI18N
        saveRatingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveRatingButtonActionPerformed(evt);
            }
        });

        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setActionCommand(resourceMap.getString("closeButton.actionCommand")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        qualityStarRater.setName("qualityStarRater"); // NOI18N
        qualityStarRater.setStars(10);

        org.jdesktop.layout.GroupLayout qualityStarRaterLayout = new org.jdesktop.layout.GroupLayout(qualityStarRater);
        qualityStarRater.setLayout(qualityStarRaterLayout);
        qualityStarRaterLayout.setHorizontalGroup(
            qualityStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 160, Short.MAX_VALUE)
        );
        qualityStarRaterLayout.setVerticalGroup(
            qualityStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        priceStarRater.setName("priceStarRater"); // NOI18N
        priceStarRater.setStars(10);

        org.jdesktop.layout.GroupLayout priceStarRaterLayout = new org.jdesktop.layout.GroupLayout(priceStarRater);
        priceStarRater.setLayout(priceStarRaterLayout);
        priceStarRaterLayout.setHorizontalGroup(
            priceStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 160, Short.MAX_VALUE)
        );
        priceStarRaterLayout.setVerticalGroup(
            priceStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        averageRatingLabel.setFont(resourceMap.getFont("averageRatingLabel.font")); // NOI18N
        averageRatingLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        averageRatingLabel.setText(resourceMap.getString("averageRatingLabel.text")); // NOI18N
        averageRatingLabel.setName("averageRatingLabel"); // NOI18N

        serviceTimeStarRater.setName("serviceTimeStarRater"); // NOI18N
        serviceTimeStarRater.setStars(10);

        org.jdesktop.layout.GroupLayout serviceTimeStarRaterLayout = new org.jdesktop.layout.GroupLayout(serviceTimeStarRater);
        serviceTimeStarRater.setLayout(serviceTimeStarRaterLayout);
        serviceTimeStarRaterLayout.setHorizontalGroup(
            serviceTimeStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 160, Short.MAX_VALUE)
        );
        serviceTimeStarRaterLayout.setVerticalGroup(
            serviceTimeStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        averageRatingStarRater.setEnabled(false);
        averageRatingStarRater.setName("averageRatingStarRater"); // NOI18N
        averageRatingStarRater.setStars(10);

        org.jdesktop.layout.GroupLayout averageRatingStarRaterLayout = new org.jdesktop.layout.GroupLayout(averageRatingStarRater);
        averageRatingStarRater.setLayout(averageRatingStarRaterLayout);
        averageRatingStarRaterLayout.setHorizontalGroup(
            averageRatingStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 160, Short.MAX_VALUE)
        );
        averageRatingStarRaterLayout.setVerticalGroup(
            averageRatingStarRaterLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(44, 44, 44)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                    .add(32, 32, 32)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                                        .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(averageRatingLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(qualityStarRater, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(priceStarRater, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(serviceTimeStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(89, 89, 89))
                        .add(layout.createSequentialGroup()
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 228, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(restNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 207, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(193, 193, 193)
                        .add(saveRatingButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(194, 194, 194)
                        .add(closeButton)
                        .add(148, 148, 148)))
                .add(51, 51, 51))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .add(averageRatingStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(169, 169, 169))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(restNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(51, 51, 51)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(qualityStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel4)
                    .add(priceStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(serviceTimeStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .add(39, 39, 39)
                .add(saveRatingButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(averageRatingLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14)
                .add(averageRatingStarRater, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(16, 16, 16)
                .add(closeButton)
                .add(80, 80, 80))
        );

        saveRatingButton.getAccessibleContext().setAccessibleName(resourceMap.getString("jButton1.AccessibleContext.accessibleName")); // NOI18N
        closeButton.getAccessibleContext().setAccessibleName(resourceMap.getString("jButton2.AccessibleContext.accessibleName")); // NOI18N
        averageRatingLabel.getAccessibleContext().setAccessibleName(resourceMap.getString("averageRatingLabel.AccessibleContext.accessibleName")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        root.setEnabled(true);
        dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void saveRatingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveRatingButtonActionPerformed


        //αν δεν υπάρχει αυτή η αξιολόγηση στην βάση 
        if (dBM.checkIfRatingExist(restId, username) == false) {
            // αν δεν έχουμε επιλέξει όλες τις κατηγορίες τ΄οε εμφανίζει μήνυμα
            if (qualityStarRater.getSelection() == 0 || priceStarRater.getSelection() == 0 || serviceTimeStarRater.getSelection() == 0) {

                JOptionPane.showMessageDialog(this, "Δεν έχετε αξιολογήσει τουλάχιστον μια κατηγορία!");
                // αλλίως δημιουργεί μια εγγραφή στην βάση
            } else {

                Rating r = new Rating();
                r.setRestId(restId);
                r.setUsername(username);
                r.setQuality(qualityStarRater.getSelection());
                r.setPrice(priceStarRater.getSelection());
                r.setServiceTime(serviceTimeStarRater.getSelection());
                r.setAverageRating((r.getQuality() + r.getPrice() + r.getServiceTime()) / 3);

                averageRatingLabel.setText(df.format(r.getAverageRating()) + "/10");
                averageRatingStarRater.setRating(r.getAverageRating());
                averageRatingStarRater.setRating(averageRatingStarRater.getRating());
                //αν η αποθήκευση γινεί χωρις πρόβλημα τότε εμφανίζει μήνυμα επιτυχίας.
                if (dBM.storeRating(r) == 1) {

                    JOptionPane.showMessageDialog(this, "H αξιολόγηση καταχωρήθηκε με επιτυχία.");
                    // αλλιώς εμφανίζει μήνυμα λάθους
                } else {

                    JOptionPane.showMessageDialog(this, "Παρουσιάστηκε κάποιο πρόβλημα στην καταχώρηση της αξιολόγηση");

                }

            }
            //αν υπάρχει αξιολόγηση τότε ενημερωνει την βαση σε τυχον αλλαγες που κανει ο χρηστης
        } else {
            
            Rating r = new Rating();
            r.setRestId(restId);
            r.setUsername(username);
            if (qualityStarRater.getSelection() == 0){
                r.setQuality(qualityStarRater.getRating());
            }else{
                r.setQuality(qualityStarRater.getSelection());
            }
            if (priceStarRater.getSelection() == 0){
                r.setPrice(priceStarRater.getRating());
            }else{
                r.setPrice(priceStarRater.getSelection());
            }
            if (serviceTimeStarRater.getSelection() == 0){
                r.setServiceTime(serviceTimeStarRater.getRating());
            }else{
                r.setServiceTime(serviceTimeStarRater.getSelection());
            }
            r.setAverageRating((r.getQuality() + r.getPrice() + r.getServiceTime()) / 3);

            averageRatingLabel.setText(df.format(r.getAverageRating()) + "/10");
            averageRatingStarRater.setRating(r.getAverageRating());
            averageRatingStarRater.setRating(averageRatingStarRater.getRating());
            //αν η αποθήκευση γινεί χωρις πρόβλημα τότε εμφανίζει μήνυμα επιτυχίας.
            if (dBM.updateRating(r) == 1) {

                JOptionPane.showMessageDialog(this, "H αξιολόγηση ενημερώθηκε με επιτυχία.");
           // αλλιώς εμφανίζει μήνυμα λάθους
            } else {

                JOptionPane.showMessageDialog(this, "Παρουσιάστηκε κάποιο πρόβλημα στην ενημέρωση της αξιολόγηση");

            }
        }

    }//GEN-LAST:event_saveRatingButtonActionPerformed

    public void setRoot(SearchRes root){
        this.root=root;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RatingRest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RatingRest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RatingRest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RatingRest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            }
        });



    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel averageRatingLabel;
    private handax.utilities.StarRater averageRatingStarRater;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private handax.utilities.StarRater priceStarRater;
    private handax.utilities.StarRater qualityStarRater;
    private javax.swing.JLabel restNameLabel;
    private javax.swing.JButton saveRatingButton;
    private handax.utilities.StarRater serviceTimeStarRater;
    // End of variables declaration//GEN-END:variables
}
